//
//  HXAllProfessionViewController.m
//  GiftSay
//
//  Created by laohu on 16/1/9.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "HXAllProfessionViewController.h"
#import "HXAllCagatoryTableViewCell.h"
#import "HXTactikFirstModel.h"
#import "HXInsideAllProfessionViewController.h"
@interface HXAllProfessionViewController ()

@end

@implementation HXAllProfessionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    NSString *url = [kAllProfession stringByAppendingString:@"offset=0"];
    [self getData:kAllProfession];
    if (self.allProfession.count > 0) {
        [self createView];
    }
    self.navigationItem.rightBarButtonItem = nil;
    
    self.navigationItem.title = @"全部专题";
    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backToCatagory:)];
    self.navigationItem.leftBarButtonItem = button;
    // Do any additional setup after loading the view.

}


- (void)backToCatagory:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:NO];
}


- (void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 64) style:UITableViewStyleGrouped];
    
//    self.tableView.backgroundColor = [UIColor redColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[HXAllCagatoryTableViewCell class] forCellReuseIdentifier:@"AllCatagoryCell"];
    [self.view addSubview:_tableView];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 0.3 * self.view.height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allProfession.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXAllCagatoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AllCatagoryCell"];
    cell.model = self.allProfession[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    HXInsideAllProfessionViewController *inside = [[HXInsideAllProfessionViewController alloc] init];
    inside.model = self.allProfession[indexPath.row];
    
    [self.navigationController pushViewController:inside animated:NO];
    
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    self.contenty = scrollView.contentOffset.y;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y > self.contenty) {
        if (scrollView.contentOffset.y > scrollView.contentSize.height - 700) {
            //加载更多数据
            if (self.scrollTimes == 0) {
                self.arrayCount = self.arrayCount + 20;
                //                NSInteger i = self.arrayCount / 20;
                //
                //                NSString *append = [NSString stringWithFormat:@"offset=%ld", i];
                //                NSString *url = [self.next_url stringByAppendingString:append];
                [self getData:self.next_url];
                self.scrollTimes = 1;
               
            }
            
        }
    }
}

- (void)getData:(NSString *)url {
    [LYKNetWorkTool getWithUrl:url parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id(success)) {
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
        NSDictionary *dic = success[@"data"];
        NSDictionary *diction = dic[@"paging"];
        NSArray *array = dic[@"collections"];
        for (NSDictionary *dictionary in array) {
            HXTactikFirstModel *tactikFirstModel = [[HXTactikFirstModel alloc] init];
            [tactikFirstModel setValuesForKeysWithDictionary:dictionary];
            [arr addObject:tactikFirstModel];
        }
        if (!self.allProfession) {
            self.allProfession = [NSMutableArray arrayWithArray:arr];
            self.next_url = diction[@"next_url"];
        } else {
            [self.allProfession addObjectsFromArray:arr];
            self.next_url = diction[@"next_url"];
            
        }
        
//        NSLog(@"%ld", self.allProfession.count);
        if (self.allProfession.count > 0) {
            if (!self.tableView) {
                [self createView];
            }
            
        }
        self.scrollTimes = 0;
        [self.tableView reloadData];
        
//        NSLog(@"%@", self.next_url);
        
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
